/**
 * 
 */
package com.excalibur.core.util;

import java.util.List;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/**
 * @author "Jiance Qin"
 * 
 * @date 2014年4月14日
 * 
 * @time 上午10:33:18
 * 
 * @desc
 * 
 */
public class HSSFUtils {

	/**
	 * 
	 */
	public HSSFUtils() {

	}

	/**
	 * 创建只包含一张SHEET的Excel表.
	 * 
	 * @param sheetName
	 * @param headers
	 * @param rows
	 * @return
	 */
	public static HSSFWorkbook createOnceHSSFWorkbook(String sheetName,
			String[] headers, List<String[]> rows) {

		HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
		if (StringHelper.isBlank(sheetName)) {
			sheetName = "通用OP数据表";
		}
		HSSFSheet hssfSheet = hssfWorkbook.createSheet(sheetName);
		int row = 0;
		HSSFRow hssfRow;
		HSSFCell hssfCell;
		if (headers != null && headers.length != 0) {
			hssfRow = hssfSheet.createRow(row++);
			for (int i = 0; i < headers.length; i++) {
				hssfCell = hssfRow.createCell(i);
				if (headers[i] != null) {
					hssfCell.setCellValue(headers[i]);
				} else {
					hssfCell.setCellValue("");
				}
			}
		}
		if (rows != null && !rows.isEmpty()) {
			for (int j = 0; j < rows.size(); row++, j++) {
				hssfRow = hssfSheet.createRow(row);
				String[] cells = rows.get(j);
				if (cells != null && cells.length != 0) {
					for (int k = 0; k < cells.length; k++) {
						hssfCell = hssfRow.createCell(k);
						if (cells[k] != null) {
							hssfCell.setCellValue(cells[k]);
						} else {
							hssfCell.setCellValue("");
						}
					}
				}
			}
		}
		return hssfWorkbook;
	}
}
